package com.lm.scau.dyb.inter;

import android.util.Log;

import com.lm.scau.dyb.activity.base.BaseActivity;

/**
 * Function:
 * auth：liming on 2016/8/4 16:01
 * mail：liming@wxchina.com
 */
public final class CommandTaskEvent extends CommandTask<Object, Object, Object> {

    private BasicUIEvent uiEvent;
    private PreExecuteEvent preEvent;
    private PostExecuteEvent posEvent;
    private BaseActivity context;
    private String tip;
    private boolean showLoading;

    public CommandTaskEvent(BasicUIEvent basicUIEvent, PreExecuteEvent preEvent, PostExecuteEvent posEvent) {
        this.uiEvent = basicUIEvent;
        this.posEvent = posEvent;
        this.preEvent = preEvent;
    }

    public CommandTaskEvent(BasicUIEvent uiEvent, BaseActivity context, String tip,boolean showLoading) {
        this.uiEvent = uiEvent;
        this.context = context;
        this.tip = tip;
        this.showLoading = showLoading;
    }

    @Override
    public Object doInBackground(Object... params) {
        System.out.print("===do in background===");
        uiEvent.execute(Integer.valueOf(params[0].toString()), params[1]);
        return params;
    }

    @Override
    public void onPostExecute(Object obj) {
        super.onPostExecute(obj);
        if (null != posEvent) {
            posEvent.postExecute(obj);
        }
        if(showLoading){
            context.dismiss();
        }
    }

    @Override
    public void onPreExecute(Object... params) {
        super.onPreExecute(params);
        if (null != tip && !"".equals(tip)) {
            context.setTip(tip);
        }
        if(showLoading){
            context.loading();
        }
        if (null != preEvent) {
            preEvent.preExecute(Integer.parseInt(params[0].toString()), params[1]);
        }
    }
}
